package com.example.spring_musicserver_1113.controller;

import com.example.spring_musicserver_1113.mapper.LoveMusicMapper;
import com.example.spring_musicserver_1113.model.Music;
import com.example.spring_musicserver_1113.model.User;
import com.example.spring_musicserver_1113.tools.Constant;
import com.example.spring_musicserver_1113.tools.ResponseBodyMessage;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @version 1.0
 * @Author 作者名
 * @Date 2024/11/15 15:23
 * @注释
 */
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Autowired
    private LoveMusicMapper loveMusicMapper;
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id,
                                        HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        System.out.println("musicId:"+musicId);
        //1、检查当前是否等了
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1,"请登录后进行收藏喜欢",false);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId:"+userId);
        Music music = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId,musicId);
        if(music != null){//这里加一个取消收藏的功能
            return new  ResponseBodyMessage<>(-1,"该音乐已被喜欢收藏",false);
        }
        boolean effect = loveMusicMapper.insertLoveMusic(userId,musicId);
        if(effect){
            return new ResponseBodyMessage<>(0,"收藏成功",true);
        }else {
            return new ResponseBodyMessage<>(-1,"收藏失败",false);
        }
    }
    @RequestMapping("findLoveMusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                    HttpServletRequest request){
        //1、验证当前是否登录
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1,"请登录后进行查找收藏喜欢的音乐",null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId"+userId);
        List<Music>  musicList=null;
        if(musicName == null){
            musicList =loveMusicMapper.findLoveMusicByUserId(userId);
        }else {
            musicList = loveMusicMapper.findLoveMusicByKeyAndUID(musicName,userId);
        }
        return new ResponseBodyMessage<>(0,"查询到了所有的歌曲信息",musicList);
    }

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,
                                                        HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        //验证登录
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1,"请登录后进行查找收藏喜欢的音乐",null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        int ret = loveMusicMapper.deleteLoveMusic(userId,musicId);
        if(ret == 1){
            return new ResponseBodyMessage<>(0,"取消收藏成功",true);
        }else {
            return new ResponseBodyMessage<>(-1,"取消收藏失败",false);
        }
    }
}
